<?php

class Posts_model extends CI_Model{

	function getPosts( $limit=1000 ){
		$columns = 'a.*,b.firstname,b.lastname,b.picture';
		$this->db->select($columns);
		$this->db->from('mlme_posts AS a');
		$this->db->join('mlme_users AS b', 'a.userid=b.userid', 'left');
		$this->db->where('a.status',1);
		$this->db->order_by('a.date_updated', 'asc');
		$this->db->limit($limit);
		$this->db->group_by('a.postid');
		$query = $this->db->get();
		
		return ($query->num_rows() > 0) ? $query->result() : FALSE ;
	}
	function get_userid($email){
		 $query = $this->db->query("SELECT userid FROM mlme_users WHERE email ='".$email."'");
		 $result = $query->row();
		 return $result->userid;
	}
	function get_remembrance_wall(){
		$query = $this->db->query("SELECT a.*,b.firstname,b.lastname FROM mlme_posts a LEFT JOIN mlme_users b ON a.userid = b.userid ORDER BY a.date_added DESC");
		return $query->result_array();
	}
	function save_remembrance_wall($post,$c_date,$birthday,$date_of_death){
		$data = array(
			 'userid'   => $this->get_userid($_SESSION['email']),
			 'message'  => $post['add-remwall-msg'],
			 'post_to'  => $post['add-remwall-name'],
			 'birthday' => $birthday,
		'date_of_death' => $date_of_death,
			 'files'     => $post['file_name'],
		   'date_added' => $c_date,
		 'date_updated' => $c_date,
			   'status' => 1
		);
		$insert = $this->db->insert('mlme_posts',$data);
		return $insert;

	}
}